/*
 * CsvLoader.cpp
 *
 *  Created on: 31.10.2012
 *      Author: fs
 */

#include "CsvLoader.h"
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <vector>
#include <iterator>

using namespace std;

#define BUFFER_SIZE 255

CsvLoader::CsvLoader()
{
	this->dataItems = NULL;
}

CsvLoader::~CsvLoader()
{
		delete[] dataItems->items;
		delete[] dataItems;
}

int CsvLoader::loadFile(std::string fileName)
{
	char buffer[255];	// buffer
	unsigned int size = 1;		// velikost pole pro gb a ig

	FILE* inputFile = fopen(fileName.c_str(), "r");

	if (inputFile == NULL)
	{
		cerr << "Can't open input file!" << endl;
		return EXIT_FAILURE;
	}

	fgets(buffer, BUFFER_SIZE, inputFile);			// skip head

	char* modeValue = NULL;

	unsigned int startIndex = 0;
	while (modeValue == NULL)		// skip unshared part
	{
		fgets(buffer, BUFFER_SIZE, inputFile);

		strtok(buffer, ";");
		strtok(NULL, ";");
		strtok(NULL, ";");
		modeValue = strtok(NULL, ";");
		startIndex++;
	}

	while (fgets(buffer, BUFFER_SIZE, inputFile))		// count shared items
	{
		size++;
	}

	rewind(inputFile);

	this->dataItems = new data[1];
	this->dataItems->items = new item[size];

	size = 0;
	int index = 0;
	bool startSaving = false;

	while (fgets(buffer, BUFFER_SIZE, inputFile))		// load shared data
	{
		double bg;
		double ig;
		int time;

		if (size == startIndex)
		{
			startSaving = true;
		}
		else
		{
			size++;
		}

		if (startSaving)
		{
			this->replaceDecimalSepar(buffer);
			this->parseLine(buffer, &time, &bg, &ig);

			this->dataItems->items[index].bg = bg;
			this->dataItems->items[index].ig =  ig;
			this->dataItems->items[index].ecg = bg - ig;
			this->dataItems->items[index].t = time;
			this->dataItems->items[index].time =  time / (24*60*60.0) ;

			dataItems->length = index;
			index++;
		}
	}

	fclose(inputFile);
	return EXIT_SUCCESS;
}

data* CsvLoader::getData()
{
	return this->dataItems;
}

void CsvLoader::parseLine(char* line, int* time, double* bg, double* ig)
{
	char buffer[255];
	int delimiter_pos = 0;
	int field_cnt = 0;

	while (delimiter_pos > -1) {
		delimiter_pos = nextLine(line, buffer, delimiter_pos);

		if (field_cnt == 0)
		{
			*time = strigToInt(buffer);
		}

		if (field_cnt == 1)
		{
			*bg = atof(buffer);
		}

		if (field_cnt == 2)
		{
			*ig = atof(buffer);
		}

		field_cnt++;
	}
}

int CsvLoader::nextLine(char* line, char* result, int start_index)
{
	int cnt = start_index;
	int i=0;
	while (line[cnt] != '\0' && line[cnt] != '\n' && line[cnt] !=';') {
		result[i] = line[cnt];
		i++;
		cnt++;
	}
	result[i] = '\0';
	if (line[cnt] == '\n' || line[cnt] == '\0') { //last field
		return -1;
	}
	return cnt+1;
}


void CsvLoader::replaceDecimalSepar(char* chars)
{
	while (*chars != '\0')
	{
		if (*chars == ',')
		{
			*chars = '.';
		}
		chars++;
	}
}

int CsvLoader::strigToInt(char* time)
{
	int hours = 0, mins = 0, secs = 0;
	char buffer[255];

	int buffer_i = 0;
	int line_i = 0;
	int field_cnt = 0;

	while (true)
	{
		buffer[buffer_i] = time[line_i];
		if (time[line_i] == ':' || time[line_i] == '\0')
		{
			buffer[buffer_i] = '\0';
			switch (field_cnt)
			{
			case 0:
				hours = atoi(buffer);
				break;
			case 1:
				mins = atoi(buffer);
				break;
			case 2:
				secs = atoi(buffer);
				break;
			}
			buffer_i = -1;
			field_cnt++;
		}

		if (time[line_i] == '\0')
		{
			break;
		}

		buffer_i++;
		line_i++;
	}

	int retTime = hours * 60 * 60 + mins * 60 + secs;
	return retTime;
}
